//
//  RadarView.m
//  radarTest
//
//  Created by SC on 16/7/29.
//  Copyright © 2016年 SC. All rights reserved.
//

#import "RadarView.h"
#define WIDTH ([UIScreen mainScreen].bounds.size.width)
#define HEIGHT ([UIScreen mainScreen].bounds.size.height)

@interface RadarView (){
    UIImageView *_image1;
    UIImageView *_image2;
    UIImageView *_image3;
    UIImageView *_image4;
    UIImageView *_image5;
    
    double animationDuration;
}

@property (nonatomic, strong) CALayer * animationLayer;
@property (nonatomic, strong) UIImage *thumbnailImage;//用户缩略图（中心图）

@end

@implementation RadarView

-(instancetype)init{
    self = [super init];
    if (self) {
        self.frame = CGRectMake(0, 0, WIDTH, WIDTH);
        
        self.thumbnailImage = [UIImage imageNamed:@"注册-雷达中间"];
        
    }
    return self;
}

- (void)layoutSubviews{
    [super layoutSubviews];
    self.backgroundColor = [UIColor clearColor];
}

- (void)drawRect:(CGRect)rect {
    [[UIColor clearColor]setFill];//view的背景色
    UIRectFill(rect);
    NSInteger pulsingCount = 4;//雷达上波纹的条数
    animationDuration = 4;//一组动画持续的时间，直接决定了动画运行快慢
    
    CALayer * animationLayer = [[CALayer alloc]init];
    self.animationLayer = animationLayer;
    
    for (int i = 0; i < pulsingCount; i++) {
        CALayer * pulsingLayer = [[CALayer alloc]init];
        pulsingLayer.frame = CGRectMake(0, 0, rect.size.width, rect.size.height);
        pulsingLayer.backgroundColor = [UIColor whiteColor].CGColor;//圈圈背景颜色，不设置则为透明。
        pulsingLayer.borderColor = [UIColor blueColor].CGColor;
        pulsingLayer.borderWidth = 1.0;
        pulsingLayer.cornerRadius = rect.size.height/2;
        
        
        CAMediaTimingFunction * defaultCurve = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn];
        
        CAAnimationGroup * animationGroup = [[CAAnimationGroup alloc]init];
        animationGroup.fillMode = kCAFillModeBoth;
        //因为雷达中每个圈圈的大小不一致，故需要他们在一定的相位差的时刻开始运行。
        animationGroup.beginTime = CACurrentMediaTime() + (double)i * animationDuration/(double)pulsingCount;
        
        animationGroup.duration = animationDuration;//每个圈圈从生成到消失使用时常，也即动画组每轮动画持续时常
        animationGroup.repeatCount = HUGE_VAL;//表示动画组持续时间为无限大，也即动画无限循环。
        animationGroup.timingFunction = defaultCurve;
        
        [self addUserIcon];
        
        
        //雷达圆圈初始大小以及最终大小比率。
        CABasicAnimation * scaleAnimation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
        scaleAnimation.autoreverses = NO;
        scaleAnimation.fromValue = [NSNumber numberWithDouble:0.1];
        scaleAnimation.toValue = [NSNumber numberWithDouble:1.0];
        
        //雷达圆圈在n个运行阶段的透明度，n为数组长度。
        CAKeyframeAnimation * opacityAnimation = [CAKeyframeAnimation animationWithKeyPath:@"opacity"];
        //雷达运行四个阶段不同的透明度。
        opacityAnimation.values = @[[NSNumber numberWithDouble:1.0],[NSNumber numberWithDouble:0.5],[NSNumber numberWithDouble:0.3],[NSNumber numberWithDouble:0.0]];
        //雷达运行的不同的四个阶段，为0.0表示刚运行，0.5表示运行了一半，1.0表示运行结束。
        opacityAnimation.keyTimes = @[[NSNumber numberWithDouble:0.0],[NSNumber numberWithDouble:0.25],[NSNumber numberWithDouble:0.5],[NSNumber numberWithDouble:1.0]];
        //将两组动画（大小比率变化动画，透明度渐变动画）组合到一个动画组。
        animationGroup.animations = @[scaleAnimation,opacityAnimation];
        
        [pulsingLayer addAnimation:animationGroup forKey:@"pulsing"];
        [animationLayer addSublayer:pulsingLayer];
        
    }
    self.animationLayer.zPosition = -1;//重新加载时，使动画至底层
    [self.layer addSublayer:self.animationLayer];
    
    
    //以下部分为雷达中心的用户缩略图。雷达圈圈也是从该图中心发出。
    CALayer * thumbnailLayer = [[CALayer alloc]init];
    thumbnailLayer.backgroundColor = [UIColor whiteColor].CGColor;
    CGRect thumbnailRect = CGRectMake(0, 0, 46, 46);
    thumbnailRect.origin.x = (rect.size.width - thumbnailRect.size.width)/2.0;
    thumbnailRect.origin.y = (rect.size.height - thumbnailRect.size.height)/2.0;
    thumbnailLayer.frame = thumbnailRect;
    thumbnailLayer.cornerRadius = 23.0;
    thumbnailLayer.borderWidth = 1.0;
    thumbnailLayer.masksToBounds = YES;
    thumbnailLayer.borderColor = [UIColor whiteColor].CGColor;
    UIImage * thumbnail = self.thumbnailImage;
    thumbnailLayer.contents = (id)thumbnail.CGImage;
    thumbnailLayer.zPosition = -1;
    [self.layer addSublayer:thumbnailLayer];

}

-(void)addUserIcon{
    
    CGPoint center = CGPointMake(self.bounds.size.width/2, self.bounds.size.width/2);
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(animationDuration*0.25 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        
        _image1 = [[UIImageView alloc] init];
        _image1.frame= CGRectMake(center.x*1.2, center.y*1.2, 40, 40);
        _image1.contentMode = UIViewContentModeScaleAspectFit;
        _image1.image = [UIImage imageNamed:@"注册-雷达－头像01"];
        _image1.alpha = 0;
        [self addSubview:_image1];
        
        UILabel *label1 = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 50, 20)];
        label1.text = @"小明";
        label1.textColor = [UIColor colorWithHexString:@"283345" alpha:1];
        label1.font = [UIFont systemFontOfSize:12];
        label1.textAlignment = NSTextAlignmentCenter;
        label1.center = CGPointMake(_image1.center.x, _image1.center.y-25);
        label1.alpha = 0;
        [self addSubview:label1];
        
        [UIView animateWithDuration:3 animations:^{
            _image1.alpha = 1;
            label1.alpha = 1;
        }];
        
        
    });
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(animationDuration*0.25 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        
        _image2 = [[UIImageView alloc] init];
        _image2.frame= CGRectMake(center.x*1.3, center.y*0.8, 40, 40);
        _image2.contentMode = UIViewContentModeScaleAspectFit;
        _image2.image = [UIImage imageNamed:@"注册-雷达－头像02"];
        _image2.alpha = 0;
        [self addSubview:_image2];
        
        UILabel *label2 = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 50, 20)];
        label2.text = @"小王";
        label2.textColor = [UIColor colorWithHexString:@"283345" alpha:1];
        label2.font = [UIFont systemFontOfSize:12];
        label2.textAlignment = NSTextAlignmentCenter;
        label2.center = CGPointMake(_image2.center.x, _image2.center.y-25);
        label2.alpha = 0;
        [self addSubview:label2];
        
        [UIView animateWithDuration:3 animations:^{
            _image2.alpha = 1;
            label2.alpha = 1;
        }];
        
        
    });
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(animationDuration*0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        
        _image3 = [[UIImageView alloc] init];
        _image3.frame= CGRectMake(center.x*1.4, center.y*1.4, 40, 40);
        _image3.contentMode = UIViewContentModeScaleAspectFit;
        _image3.image = [UIImage imageNamed:@"注册-雷达－头像03"];
        _image3.alpha = 0;
        [self addSubview:_image3];
        
        UILabel *label3 = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 50, 20)];
        label3.text = @"小唐";
        label3.textColor = [UIColor colorWithHexString:@"283345" alpha:1];
        label3.font = [UIFont systemFontOfSize:12];
        label3.textAlignment = NSTextAlignmentCenter;
        label3.center = CGPointMake(_image3.center.x, _image3.center.y-25);
        label3.alpha = 0;
        [self addSubview:label3];
        
        [UIView animateWithDuration:3 animations:^{
            _image3.alpha = 1;
            label3.alpha = 1;
        }];
        
        
    });
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(animationDuration*0.75 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        
        _image4 = [[UIImageView alloc] init];
        _image4.frame= CGRectMake(center.x*0.7, center.y*0.6, 40, 40);
        _image4.contentMode = UIViewContentModeScaleAspectFit;
        _image4.image = [UIImage imageNamed:@"注册-雷达－头像04"];
        _image4.alpha = 0;
        [self addSubview:_image4];
        
        UILabel *label4 = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 50, 20)];
        label4.text = @"小陈";
        label4.textColor = [UIColor colorWithHexString:@"283345" alpha:1];
        label4.font = [UIFont systemFontOfSize:12];
        label4.textAlignment = NSTextAlignmentCenter;
        label4.center = CGPointMake(_image4.center.x, _image4.center.y-25);
        label4.alpha = 0;
        [self addSubview:label4];
        
        [UIView animateWithDuration:3 animations:^{
            _image4.alpha = 1;
            label4.alpha = 1;
        }];
        
        
    });
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(animationDuration * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        
        _image5 = [[UIImageView alloc] init];
        _image5.frame= CGRectMake(center.x*0.6, center.y*1.4, 40, 40);
        _image5.contentMode = UIViewContentModeScaleAspectFit;
        _image5.image = [UIImage imageNamed:@"注册-雷达－头像05"];
        _image5.alpha = 0;
        [self addSubview:_image5];
        
        UILabel *label5 = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 50, 20)];
        label5.text = @"小赵";
        label5.textColor = [UIColor colorWithHexString:@"283345" alpha:1];
        label5.font = [UIFont systemFontOfSize:12];
        label5.textAlignment = NSTextAlignmentCenter;
        label5.center = CGPointMake(_image5.center.x, _image5.center.y-25);
        label5.alpha = 0;
        [self addSubview:label5];
        
        [UIView animateWithDuration:3 animations:^{
            _image5.alpha = 1;
            label5.alpha = 1;
        }];
        
        
    });
    
}



@end
